<template>
  <div class="ICDetail">
    <div class="flex">
      <div class="">
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/IC/search' }"
            >产业技术链</el-breadcrumb-item
          >
          <el-breadcrumb-item>{{ nodeInfo.chainName }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="">
        <p class="search-tip">
          其他产业链：<span
            v-for="item in searchList"
            :key="item.dictValue"
            @click="handleSearch(item.dictValue)"
            >{{ item.dictValue }}</span
          >
        </p>
      </div>
    </div>
    <div class="tabs-con">
      <el-tabs class="icd-tabs" v-model="activeName" @tab-click="handleClick">
        <el-tab-pane :label="nodeInfo.chainName" name="first">
          <Chain :nodeInfo="nodeInfo" @tabChange="tabChange" />
        </el-tab-pane>
        <el-tab-pane label="产业相关数据" name="second" lazy>
          <p class="pnode">
            产业节点：<span class="node">{{
              nodeInfo.name || nodeInfo.chainName
            }}</span>
          </p>
          <Industry :showTabs="showTabs" :nodeInfo="nodeInfo" />
        </el-tab-pane>
      </el-tabs>
      <div class="btn">
        <!-- <el-button type="primary" size="small" disabled>导出报告</el-button> -->
        <el-button
          v-if="!showSubBtn"
          type="primary"
          size="small"
          @click="handleSub"
          :loading="subLoading"
          >订阅</el-button
        >
        <el-button v-else size="small" @click="handleCancel"
          >取消订阅</el-button
        >
      </div>
    </div>
  </div>
</template>
<script>
import Chain from "./chain.vue";
import Industry from "./industry.vue";
export default {
  data() {
    return {
      userInfo: {},
      activeName: "first",
      searchList: [],
      showTabs: "first",
      nodeInfo: {
        chainName: "",
        eid: "",
        name: "",
      },
      subInfo: {},
      showSubBtn: false,
      subLoading: false,
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.userInfo = JSON.parse(localStorage.getItem("homeUserInfo"));
      const query = this.$route.query;
      // console.log("query", query);
      this.nodeInfo.chainName = query.dictLabel || "";
      this.nodeInfo.name = query.name || "";
      this.nodeInfo.eid = query.eid || "";
      this.initData(query.dictLabel);
    },
    // 其他产业链数据
    initData(val) {
      this.$router.push({
        query: { ...this.$route.query, dictLabel: val },
      });
      const data = JSON.parse(localStorage.getItem("allDict"));
      const industryChain = data.find((list) => {
        return list.dictType == "industry_chain";
      }).dataList;
      // const industryChain = JSON.parse(localStorage.getItem("industryChain"));
      const list = [];
      industryChain.forEach((chain) => {
        if (chain.dictLabel != this.nodeInfo.chainName) {
          list.push(chain);
        }
      });
      this.searchList = list;
      this.isSub();
    },
    handleClick(tab, event) {
      // console.log(tab, event);
    },
    handleSearch(val) {
      // this.$message({
      //   type: "warning",
      //   message: "开发中，敬请等待！",
      // });
      // return;
      Object.assign(this.nodeInfo, {
        chainName: val,
        eid: "",
        name: "",
      });
      this.initData(val);
    },
    tabChange(str, nodeInfo) {
      // if (nodeInfo) {
      //   this.nodeInfo = nodeInfo;
      // }
      Object.assign(this.nodeInfo, nodeInfo);
      this.activeName = "second";
      this.showTabs = str;
    },
    handleSub() {
      if (this.subLoading) {
        return;
      }
      this.subLoading = true;
      this.$post(this.$Url.personal.chainSub, {
        userId: this.userInfo.id,
        chainName: this.nodeInfo.chainName,
      })
        .then((res) => {
          this.$message({
            type: "success",
            message: "订阅成功！",
          });
          this.showSubBtn = true;
          this.subLoading = false;
          this.subInfo = {
            id: res.data,
          };
        })
        .catch((err) => {
          this.subLoading = false;
        });
    },
    isSub() {
      this.$get(this.$Url.personal.chainIsSub, {
        userId: this.userInfo.id,
        chainName: this.nodeInfo.chainName,
      }).then((res) => {
        // console.log(res.data);
        this.subInfo = res.data;
        this.showSubBtn = res.data;
      });
    },
    handleCancel() {
      this.$confirm("您确定取消订阅吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        this.$get(this.$Url.personal.canalChain, {
          id: this.subInfo.id,
        }).then((res) => {
          this.$message({
            type: "success",
            message: "取消订阅成功！",
          });
          this.showSubBtn = false;
        });
      });
    },
  },
  components: {
    Chain,
    Industry,
  },
};
</script>
<style lang="scss">
.el-message-box{
  .el-button--primary {
    color: #FFF;
    background-color: $mainColor !important;
    border-color: $mainColor !important;
}
.el-button--primary:active,.el-button--primary:focus,.el-button--primary:hover{
  color: #FFF !important;
    background-color: $mainColor !important;
    border-color: $mainColor !important;
}
.el-button:focus, .el-button:hover {
    color: $mainColor;
    border-color: $mainColor;
    background-color: $hoverColor;
}
}
</style>
<style lang="scss" scoprd>
.ICDetail {
  padding: 30px;
  background-color: $color46;
  .flex {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    .search-tip {
      span {
        margin-right: 15px;
        color: $mainColor;
        cursor: pointer;
      }
    }
  }
  .icd-tabs {
    & > .el-tabs__header {
      width: 800px;
      margin: 0 auto 15px;
      .el-tabs__nav {
        width: 100%;
      }
      .el-tabs__item {
        width: 50%;
        text-align: center;
      }
    }
  }
  .el-tabs__content {
    background-color: $color46;
  }
  .pnode {
    background-color: #fff;
    padding: 15px 30px;
    .node {
      color: $mainColor;
    }
  }
  .tabs-con {
    position: relative;
    .btn {
      position: absolute;
      top: 0;
      right: 20px;
      .el-button {
        width: 80px;
        background:$mainColor !important;
        color: #fff !important;
      }
    }
  }
}
</style>
